<?php

namespace common\widgets\grid;

use common\helpers\Html;
use common\widgets\GridView;
use kartik\base\Config;
use yii\helpers\ArrayHelper;

class DataColumn extends \kartik\grid\DataColumn
{

    /**
     * {@inheritdoc}
     */
    protected function renderFilterCellContent()
    {
        $content = parent::renderFilterCellContent();
        $chkType = ! empty($this->filterType) && $this->filterType !== GridView::FILTER_CHECKBOX &&
            $this->filterType !== GridView::FILTER_RADIO && ! class_exists($this->filterType);
        if ($this->filter === false || empty($this->filterType) || $content === $this->grid->emptyCell || $chkType)
        {
            return $content;
        }
        $widgetClass = $this->filterType;
        $options = [
            'model'     => $this->grid->filterModel,
            'attribute' => $this->attribute,
            'options'   => $this->filterInputOptions,
        ];
        if (is_array($this->filter))
        {
            if (Config::isInputWidget($this->filterType) && $this->grid->pjax)
            {
                $options['pjaxContainerId'] = $this->grid->pjaxSettings['options']['id'];
            }
            if ($this->filterType === GridView::FILTER_SELECT2 || $this->filterType === GridView::FILTER_TYPEAHEAD)
            {
                $options['data'] = $this->filter;
            }
            if ($this->filterType === GridView::FILTER_RADIO)
            {
                return Html::activeRadioList(
                    $this->grid->filterModel,
                    $this->attribute,
                    $this->filter,
                    $this->filterInputOptions
                );
            }
        }
        if ($this->filterType === GridView::FILTER_CHECKBOX)
        {
            return Html::activeCheckbox($this->grid->filterModel, $this->attribute, $this->filterInputOptions);
        }
        $options = ArrayHelper::merge($this->filterWidgetOptions, $options);
        /** @var \kartik\base\Widget $widgetClass */
        return $widgetClass::widget($options);
    }
}
